﻿using Shiloh.Job.Model.Entities;
using Shiloh.Job.Repositories;
using Shiloh.Job.Services.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shiloh.Job.Services.Implementations
{
    public class IndividualLoginStrategy : ILoginStrategy
    {
        private readonly ILogServce _logServcie;
        private readonly UserIndividualRepo _userIndividualRepo;
        private readonly IResumeService _resumeService;
        public IndividualLoginStrategy(ILogServce logServcie, UserIndividualRepo userIndividualRepo, IResumeService resumeService)
        {
            _logServcie = logServcie;
            _userIndividualRepo = userIndividualRepo;
            _resumeService = resumeService;
        }
        /// <summary>
        /// 保存登录日志，记录登录设备,推送一条匹配简历,清空登录失败次数
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task LoginSuccessExecuteAsync(User user)
        {
            await _logServcie.AddLoginSuccessLog(user.Id);
            await _userIndividualRepo.ClearLoginFailCount(user.Id);
            if (_resumeService.HasResume(user.Id))
            {
                //push by wechat and app
            }

        }

        /// <summary>
        /// 保存登录日志，30分钟内输错指定次数锁定账户30分钟，更新失败次数到实体表
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task LoginFailExecuteAsync(User user)
        {
            await _logServcie.AddLoginFailLog(user.Id);
            await _userIndividualRepo.UpdateLoginFailCount(user.Id);
            int count = await _logServcie.GetFailLogIn30Min();
            if (count >= User.MaxLoginFailCount)
            {
                await _userIndividualRepo.Lock(user.Id);
            }
        }
    }
}
